from rest_framework import serializers

from answers.models import Label, Question, Reply
from articles.models import Article, Channel
from recruits.models import Enterprise, Recruit
from users.models import User


class RecruitEnterprisesSerializers(serializers.ModelSerializer):

    class Meta:

        model = Recruit

        fields = ('id',)


class EnterprisesSerializers(serializers.ModelSerializer):
    '''企业收藏关联对象嵌套序列化器类'''
    # recruits = RecruitEnterprisesSerializers(label='recruits',read_only=True) # [1, 2, 3]
    class Meta:
        model = Enterprise

        fields = ('id','name','labels','logo','recruits','summary')

#===============================================================
# 收藏文章的对应嵌套序列化类模型及其内部嵌套模型
class OuterCollectedArticlesUserInnerArticlesSerizlizer(serializers.ModelSerializer):
    '''个人收藏文章嵌套'''
    class Meta:

        model = Article
        fields = ('id','title')

class OuterCollectedArticlesUserSerizlizers(serializers.ModelSerializer):
    '''个人信息嵌套序列化器类'''
    articles = OuterCollectedArticlesUserInnerArticlesSerizlizer(read_only=True,many=True)

    class Meta:

        model = User

        fields = ('id','username','avatar','articles','fans')


class OuterCollectedArticlesChannelSerializers(serializers.ModelSerializer):
    '''频道嵌套序列化器类'''
    user = OuterCollectedArticlesUserSerizlizers(label='user',read_only=True)

    name = serializers.StringRelatedField(label='name',read_only=True)

    class Meta:

        model = Channel

        fields = ('name','user')


class OuterCollectedArticlesSerializers(serializers.ModelSerializer):
    '''收藏者关联对象嵌套序列化器类'''
    collected = serializers.CharField(read_only=True,default=False)
    user = OuterCollectedArticlesUserSerizlizers(label='user',read_only=True)
    collected_users = serializers.PrimaryKeyRelatedField(read_only=True,source='collected_articles')
    class Meta:

        model = Article

        fields = ('id','title','content','createtime','user',
                  'collected_users','collected','image','visits')

#==============================================================================

#==============================================================================
# 第一层label字段模块
class OuterLabelsSerializers(serializers.ModelSerializer):
    '''外层关联对象嵌套label序列化器类'''
    class Meta:

        model = Label

        fields = ('id','label_name')
# =============================================================================
# 第一层question字段模块
class OuterQuestionsSerializers(serializers.ModelSerializer):
    '''外层关联对象嵌套questions序列化器类'''
    labels = serializers.StringRelatedField(label='labels',read_only=True,many=True)
    user = serializers.StringRelatedField(label='user',read_only=True)
    class Meta:

        model = Question

        fields = ('id','createtime','labels','reply','replyname',
                  'replytime','title','unuseful_count','useful_count','user','visits')
#======================================================================================

#======================================================================================
# 第一层answer_question字段模块

class OuterAnswerQuestionUserSerializers(serializers.ModelSerializer):
    '''外层关联对象嵌套answer_question嵌套user序列化器类'''

    class Meta:

        model = User

        fields = ('id','username','avatar')


class ReplySerializerForSubAndParent(serializers.ModelSerializer):

    user = OuterAnswerQuestionUserSerializers(read_only=True)

    class Meta:
        model = Reply
        fields = ["id", "content","createtime","useful_count","unuseful_count","user"]

class OuterAnswerQuestionSerializers(serializers.ModelSerializer):
    '''外层关联对象嵌套answer_question序列化器类'''

    user = OuterAnswerQuestionUserSerializers(read_only=True)

    subs = ReplySerializerForSubAndParent(read_only=True, many=True)

    parent = ReplySerializerForSubAndParent(read_only=True)

    class Meta:

        model = Reply

        fields = ('id','content','createtime','useful_count',
                  'problem','unuseful_count','subs','user','parent')
# ===========================================================================


class GetPersonInfoViewSerializers(serializers.ModelSerializer):
    '''个人中心序列化器类'''
    labels = OuterLabelsSerializers(label='标签列表',read_only=True,many=True)

    enterpises = EnterprisesSerializers(label='企业',read_only=True,many=True)

    questions = OuterQuestionsSerializers(label='questions',read_only=True,many=True)

    answer_question = OuterAnswerQuestionSerializers(label='answer_question',source='replies',read_only=True,many=True)

    collected_articles = OuterCollectedArticlesSerializers(label='collected_articles', read_only=True,many=True)

    class Meta:

        model = User

        fields = ('id','username','mobile','realname','birthday',
                  'sex','avatar','website','email','city','address',
                  'labels','questions','answer_question','collected_articles',
                  'enterpises')